package com.supor.aiot.food.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.food.R;
import com.supor.aiot.food.ormlite.Cache;

import java.util.ArrayList;
import java.util.List;

public class FlowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Cache> mList = new ArrayList<>();
    private Context mContext;
    private OnFlowClickListener listener;
    private boolean showMore;
    public FlowAdapter(List<Cache> list,Context context) {
        this.mContext = context;
        setShowMore(showMore,list);
    }

    public void setOnFlowListener(OnFlowClickListener listener){
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==2||viewType==3){
            return new FlowAdapter.ImgHolder(View.inflate(mContext, R.layout.item_flex_img, null));
        }else {
            return new FlowAdapter.MyHolder(View.inflate(mContext, R.layout.item_flex_text, null));
        }
    }

    //删除positon位置的数据
    public void removeData(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mList.size() - position);
    }

    //删除全部数据
    public void removeAllData() {
        mList.clear();
        notifyItemRangeRemoved(0, mList.size());
        notifyDataSetChanged();
    }

    //在0位置添加数据
    public void insertData(Cache cache){
        if(cache!=null && !TextUtils.isEmpty(cache.getCache())){
            if(mList.size() == 0){
                mList.add(cache);
                notifyItemInserted(0);
                notifyItemRangeChanged(0, mList.size());
            }else{
                if(mList.size() > 100){
                    return;
                }
                for(Cache ele: mList){
                    if(cache.getCache().equals(ele.getCache())){
                        return;
                    }
                }
                mList.add(cache);
                /*notifyItemInserted(0);
                notifyItemRangeChanged(0,list.size());*/
                notifyDataSetChanged();
            }
        }
    }

    public void setShowMore(boolean showMore,List<Cache> list){
        this.showMore=showMore;
        mList.clear();
        if (!showMore&&list.size()>10){
            for (int i=0;i<list.size();i++){
                if (i<10)this.mList.add(list.get(i));
            }
        }else {
            this.showMore=true;
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (!showMore&&position== mList.size()){
            ImageView ivMore = ((ImgHolder) holder).ivMore;
            ivMore.setImageResource(R.mipmap.show_more);
            ivMore.setOnClickListener(view -> {
                showMore=!showMore;
                listener.OnShowHideClick(showMore);
            });
        }else {
            if (mList.size()>10&&position== mList.size()){
                ImageView ivMore = ((ImgHolder) holder).ivMore;
                ivMore.setImageResource(R.mipmap.hide_more);
                ivMore.setOnClickListener(view -> {
                    showMore=!showMore;
                    listener.OnShowHideClick(showMore);
                });
            }else {
                TextView textView = ((FlowAdapter.MyHolder) holder).text;
                textView.setText(mList.get(position).getCache());
                textView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
                    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            //长按删除
                            listener.OnItemLongClick(mList.get(position));
                            return true;
                        }
                    });
                    holder.itemView.setOnClickListener(v -> {
                        //短按
                        listener.OnItemClick(position);
                    });
                });
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (!showMore&&mList.size()==10&&position==mList.size()){
            return 2;
        }else {
            if (mList.size()>10&&position==mList.size()){
                return 3;
            }else {
                return 1;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (!showMore&& mList.size()==10){
            return mList.size()+1;
        }else {
            if (mList.size()>10){
                return mList.size()+1;
            }
            return mList.size();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private TextView text;

        public MyHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.tv_flex_desc);
        }
    }

    class ImgHolder extends RecyclerView.ViewHolder {

        private ImageView ivMore;

        public ImgHolder(View itemView) {
            super(itemView);
            ivMore = itemView.findViewById(R.id.iv_showmore);
        }
    }


    public interface OnFlowClickListener {
        void OnItemClick(int position);
        void OnItemLongClick(Cache date);
        void OnShowHideClick(boolean state);
    }

}
